package com.bishe.demo.service;

import com.bishe.demo.mapper.StoreMapper;
import com.bishe.demo.pojo.Store;
import com.bishe.demo.pojo.vo.Status;
import com.bishe.demo.utils.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ShopService {

    @Autowired
    StoreMapper storeMapper;

    //接收进货信息，插入到Store
    public Status addStore(String commodityName, Integer commodityNumber,
                           String commodityCategory,
                           Integer commodityCost, Integer commodityPrice){
        Status status = Judge.judge(commodityName,commodityNumber,commodityCategory,commodityCost,commodityPrice);
        if (!status.getMsg().equals("成功")){
            System.out.println(status.getMsg());
            System.out.println("我执行了——————————————————————");
            return status;
        }
        //1.如果仓库中没有该商品则直接添加
        HashMap<String,Object> map = new HashMap<>();
        map.put("commodity_name",commodityName);
        List<Store> list = storeMapper.selectByMap(map);
        if (list.isEmpty()){
            //空代表仓库中没有，直接插入
            Store store = new Store();
            store.setCommodityNumber(commodityNumber);
            store.setCommodityCategory(commodityCategory);
            store.setCommodityCost(commodityCost);
            store.setCommodityName(commodityName);
            store.setCommodityPrice(commodityPrice);
            storeMapper.insert(store);
        }else {
            //不空，则直接更新数量就行
            Store store = new Store();
            store.setCommodityNumber(list.get(0).getCommodityNumber()+commodityNumber);
            store.setStoreId(list.get(0).getStoreId());
            storeMapper.updateById(store);
        }
        status.setMsg("添加成功!");
        status.setCode(true);
        return status;
    }

    //退货
    public Status deleteShop(Integer storeId){
        Status status = new Status();
        if (storeId==null){
            status.setMsg("没有货单号可删除！");
            status.setCode(false);
            return status;
        }
        HashMap<String,Object> map =new HashMap<>();
        map.put("store_id",storeId);
        storeMapper.deleteByMap(map);

        status.setCode(true);
        status.setMsg("退货成功！");
        return status;
    }

}
